<!-- 检查项管理-添加&编辑页 -->
<template>
  <view class="s-list">
    <Header :title="title" bgColor="#fff" :leftIconSize="20"/>
    <view class='s-list-main'>
      <!-- 表单 -->
      <view class='li-form'>
        <uv-form labelPosition="left" :model="formData" ref="rForm" :rules="rules">
          <view class="form-top-tit">
            <view class="form-tit-text">
              检查项内容
              <text class="red-star">*</text>
            </view>
          </view>
          <view class='infoTextArea'>
            <uv-textarea v-model="formData.content" count placeholder="请输入检查项内容" maxlength="200"/>
            <view class="fontCount" @click="showSpeech">
              <image :src="imgMinioPath + '/law/yuyin.png'"></image>
            </view>
          </view>
          <uv-form-item prop="sort">
            <template #label>
              <div class="requireStar">排序</div>
            </template>
            <uv-input v-model="formData.sort" placeholder="请输入排序" border="none" maxlength="5"/>
          </uv-form-item>
        </uv-form>
      </view>
    </view>
    <!-- 底部按钮 -->
    <view class='s-btn-common'>
      <uv-button type="primary" text="确认" :disabled="loading" @click="formSubmit"/>
    </view>
    <!-- 唤起语音 -->
    <view style="height: 0">
      <uv-popup ref="refSpeech" mode="bottom" round="8">
        <speech-recognition @getSpeeckInfo="getSpeeckInfo"></speech-recognition>
      </uv-popup>
    </view>
  </view>
</template>

<script setup>
  import entryAPI from '@/api/law/entry/entry';
  import {onLoad} from '@dcloudio/uni-app';
  import {ref, reactive, getCurrentInstance, defineProps} from 'vue';
  import {validatePositiveNumber} from "../../../utils/formValidate";
  import config from "@/common/config";
  let imgMinioPath = ref(config.IMG_MINIO_PATH);
  const {proxy} = getCurrentInstance();
  const props = defineProps({
    row: {
      type: Object,
      default: () => {
        return {};
      }
    },
    editFlag: {
      required: false,
      type: Boolean,
      default: false
    }
  });

  let rForm = ref();
  let title = ref('添加');
  let loading = ref(false);
  let userInfo = ref(uni.getStorageSync('userInfo'));
  let formData = reactive({
    parentId: "0",
    parentName: "",
    content: "",
    entryType: "1",
    sort: "",
    createOrgId: userInfo.value.user.orgId,
  });
  let rules = reactive({
    sort: [
      {
        required: true,
        message: '请输入排序',
        type:'number',
        trigger: ['change']
      },
      {
        validator: (rule, value) => {
          return proxy.validate.validatePositiveNumber(value)
        },
        message: '请输入非负整数', trigger: ['blur', 'change']
      }
    ],
    content: [{
      type: 'string',
      required: true,
      message: '请输入检查项内容',
      trigger: ['change']
    }],
  });

  /** 数据加载 */
  onLoad(async (e) => {
    if (e.row) {
      e.row = JSON.parse(decodeURIComponent(e.row));
      formData.parentId = e.row.id ? e.row.id : '0';
      formData.parentName = e.row.parentName;
      formData.entryType = e.row.entryType;
    }
    if (e.editFlag) {
      title = ref('编辑');
      getData(e.row.id);
    } else {
      title = ref('添加');
    }
  });

  /** 获取数据 */
  const getData = (id) => {
    proxy.http.get(entryAPI.entryQuery(id)).then(res => {
      if (res.code === 0) {
        Object.assign(formData, res.data);
      } else {
        uni.showToast({title: res.msg, icon: 'none'});
      }
    })
  };

  /** 确认提交 */
  const formSubmit = async () => {
    loading.value = false;
    if (!formData.content) {
      await uni.showToast({title: '请输入检查项内容', icon: 'none'});
      return;
    }
    rForm.value.validate().then(() => {
      if (props.editFlag) {
        update();
      } else {
        save();
      }
    })
  };

  /** 添加功能 **/
  let save = async () => {
    uni.showModal({
      title: '温馨提示',
      content: '确定添加该条数据？',
      success: async function (res) {
        if (res.confirm) {
          let res = await proxy.http.asyncPost(entryAPI.entryAdd, formData);
          if (res.code === 0) {
            await uni.showToast({icon: 'success', title: '添加成功', duration: 2000});
            loading.value = true;
            setTimeout(() => {
              goBack();
            }, 1000)
          } else {
            await uni.showToast({title: res.msg, icon: 'none'});
          }
        }
      }
    })
  };

  /** 编辑功能 **/
  let update = async () => {
    uni.showModal({
      title: '温馨提示',
      content: '确定编辑该条数据？',
      success: async function (res) {
        if (res.confirm) {
          let res = await proxy.http.asyncPost(entryAPI.entryEdit, formData);
          if (res.code === 0) {
            await uni.showToast({icon: 'success', title: '编辑成功', duration: 2000});
            loading.value = true;
            setTimeout(() => {
              goBack();
            }, 1000)
          } else {
            await uni.showToast({title: res.msg, icon: 'none'});
          }
        }
      }
    })
  };

  /** 返回上一页 */
  let goBack = async () => {
    uni.$emit("refresh", {});
    await uni.navigateBack();
  };

  /** 语音输入 */
  const refSpeech = ref();
  const showSpeech = () => {
    refSpeech.value.open();
  };

  function getSpeeckInfo(val) {
    refSpeech.value.close();
    formData.content += val;
  }
</script>

<style scoped>

</style>
